package com.sskj.run.ui.fragment;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.run.R;
import com.sskj.run.R2;
import com.sskj.run.bean.OpenAwardBean;
import com.sskj.run.component.DaggerUserDataComponent;
import com.sskj.run.presenter.OpenAwardHistoryFragmentPresenter;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

@Route(path = RConfig.RUN_FRAGMENT_OPEN_AWARD_HISTORY)//开奖历史
public class OpenAwardHistoryFragment extends BaseFragment<OpenAwardHistoryFragmentPresenter> {
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private SlimAdapter slimAdapter;
    private MVCCoolHelper<List<OpenAwardBean>> mvcHelper;

    @Override
    protected int getLayoutId() {
        return R.layout.run_fragment_open_award_history;
    }

    @Override
    public OpenAwardHistoryFragmentPresenter getPresenter() {
        return new OpenAwardHistoryFragmentPresenter();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        refresh();
    }

    @Override
    public void initView() {
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
        });
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        slimAdapter = SlimAdapter.create().register(R.layout.run_recy_item_open_award_history, new SlimInjector<OpenAwardBean>() {
            @Override
            public void onInject(OpenAwardBean data, IViewInjector injector, List payloads) {
                if (slimAdapter.getData().indexOf(data) % 2 == 0) {
                    injector.background(R.id.llBg, ContextCompat.getDrawable(App.INSTANCE, R.drawable.run_shape_1d2_corner10));
                } else {
                    injector.backgroundColor(R.id.llBg, ContextCompat.getColor(App.INSTANCE, R.color.transparent));
                }
                injector.text(R.id.tv_periodID, data.getPeriodID())
                        .text(R.id.tv_boomValue, data.getBoomValue() + "x")
                        .text(R.id.tv_boomHash, data.getBoomHash())
                        .clicked(R.id.tv_check, view -> {
                            ARouter.getInstance().build(RConfig.RUN_CHECKOUT).withString(Constans.BOOMHASH, data.getBoomHash()).navigation();
                        });
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcHelper.setAutoLoadMore(true);
        mvcHelper.setNeedCheckNetwork(false);
        mvcHelper.setAdapter(slimAdapter);
        ModelRx2DataSource<OpenAwardBean> dataSource = new ModelRx2DataSource<>(
                (ModelRx2DataSource.OnLoadSource<OpenAwardBean>)
                        page -> mPresenter.getOpenAwardList(page, 10), 10);
        mvcHelper.setDataSource(dataSource);
        refresh();

    }


    private void refresh() {
        if (mvcHelper != null) {
            mvcHelper.refresh();
        }
    }
}
